﻿using System.Collections.Generic;
using System.Linq;

namespace Qask.Data.Core
{
    public class AccountRepository : IAccountRepository
    {
        public IList<TimeZone> GetTimezones()
        {
            using (var context = new QaskEntities())
            {
                return context.TimeZones.ToList();
            }
        }

        public int GetTimeZoneByUserId(string userId)
        {
            using(var context = new QaskEntities())
            {
                var timeZone = (from a in context.AspNetUsers
                               where a.Id == userId
                               select a.TimeZoneId).FirstOrDefault();
                return timeZone;
            }
        }

        public AspNetUser UpdateUserTimeZone(AspNetUser user)
        {
            AspNetUser updatedUser;
            using(var context = new QaskEntities())
            {
                updatedUser = context.AspNetUsers.Where(m => m.Email == user.Email).FirstOrDefault<AspNetUser>();

                if(updatedUser != null)
                {
                    updatedUser.TimeZoneId = user.TimeZoneId; 
                    context.Entry(updatedUser).State = System.Data.Entity.EntityState.Modified;                    
                    context.SaveChanges();                    
                }
                return updatedUser;
            }
        }
    }
}
